/* * Copyright (c) 2012 Daniel Huckaby * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.handlerexploit.prime.example.utils; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; import org.codehaus.jackson.JsonFactory; import org.codehaus.jackson.JsonParser; import org.codehaus.jackson.JsonToken; import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; public class Utilities { private static final String PANORAMIO_URL = "http://www.panoramio.com/map/get_panoramas.php?order=popularity&set=public&from=0&to=%s&size=%s"; public static List<Image> getPanoramioImages(Context context, int count, Size size) { return getRandomImages(context, String.format(PANORAMIO_URL, count, size.name().toLowerCase())); } public static enum Size { ORIGINAL, MEDIUM, SMALL, THUMBNAIL, SQUARE, MINI_SQUARE; } public static List<Image> getRandomThumbnailImages(Context context) { return getPanoramioImages(context, 500, Size.THUMBNAIL); } public static List<Image> getRandomLargeImages(Context context) { return getPanoramioImages(context, 500, Size.MEDIUM); } private static List<Image> getRandomImages(Context context, String source) { List<Image> images = new ArrayList<Image>(); try { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); String jsonTag = URLEncoder.encode(source); String rawJsonStringStream = sharedPreferences.getString(jsonTag, null); if (rawJsonStringStream == null) { rawJsonStringStream = toString(new URL(source).openStream()); sharedPreferences.edit().putString(jsonTag, rawJsonStringStream).commit(); } JsonFactory jsonFactory = new JsonFactory(); JsonParser jsonParser = jsonFactory.createJsonParser(rawJsonStringStream); JsonToken current; current = jsonParser.nextToken(); if (current != JsonToken.START_OBJECT) { return images; } while (jsonParser.nextToken() != JsonToken.END_OBJECT) { String fieldName = jsonParser.getCurrentName(); current = jsonParser.nextToken(); if ("photos".equals(fieldName)) { if (current == JsonToken.START_ARRAY) { while (jsonParser.nextToken() != JsonToken.END_ARRAY) { fieldName = jsonParser.getCurrentName(); jsonParser.nextToken(); Image image = new Image(); while (jsonParser.nextToken() != JsonToken.END_OBJECT) { fieldName = jsonParser.getCurrentName(); if ("photo_title".equals(fieldName)) { image.title = jsonParser.getText(); } else if ("photo_file_url".equals(fieldName)) { image.imageURL = jsonParser.getText(); } } images.add(image); } } else { jsonParser.skipChildren(); } } else { jsonParser.skipChildren(); } } jsonParser.close(); if (images.size() == 0) { sharedPreferences.edit().putString(jsonTag, null).commit(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return images; } private static String toString(InputStream input) throws IOException { final char[] buffer = new char[0x10000]; StringBuilder stringBuilder = new StringBuilder(); Reader reader = new InputStreamReader(input, "UTF-8"); int i; do { i = reader.read(buffer, 0, buffer.length); if (i > 0) { stringBuilder.append(buffer, 0, i); } } while (i >= 0); return stringBuilder.toString(); } public static class Image { public String title; public String imageURL; } }